package com.md.youyouhui.ui.shopping;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityShoppingMoreBinding;
import com.md.youyouhui.databinding.AdapterShoppingInfoBinding;
import com.md.youyouhui.net.response.GetShoppingInfoResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.ui.adapter.DataBoundViewHolder;
import com.md.youyouhui.utils.GlideApp;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/5/21.
 */
public class ShoppingMoreActivity extends BaseActivity {
    private ActivityShoppingMoreBinding moreBinding;
    private MyAdapter adapter;
    private MainViewModel viewModel;

    @Override
    public void setContentLayout() {
        moreBinding = DataBindingUtil.setContentView(this, R.layout.activity_shopping_more);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        String title = getIntent().getStringExtra("title");
        moreBinding.toolbar.setTitle(title);
        setSupportActionBar(moreBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public void initData() {
        String id = getIntent().getStringExtra("id");
        adapter = new MyAdapter();
        moreBinding.moreRecycler.setAdapter(adapter);
        getInfo(id);
    }

    private void getInfo(String id) {
        viewModel.getShoppingInfo(id).observe(this, listResource -> {
            assert listResource != null;
            switch (listResource.status) {
                case LOADING:
                    moreBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    moreBinding.progress.setVisibility(View.GONE);
                    List<GetShoppingInfoResponse> data = listResource.data;
                    if (null == data) {
                        Toast.makeText(ct, "暂无数据", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    adapter.upDate(data);
                    if (data.size() == 0) {
                        Toast.makeText(ct, "暂无数据", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case ERROR:
                    moreBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, listResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterShoppingInfoBinding>> {
        List<GetShoppingInfoResponse> data;

        void upDate(List<GetShoppingInfoResponse> data) {
            this.data = data;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public DataBoundViewHolder<AdapterShoppingInfoBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            AdapterShoppingInfoBinding binding = DataBindingUtil.inflate(LayoutInflater.from(ct), R.layout.adapter_shopping_info, viewGroup, false);
            return new DataBoundViewHolder<>(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterShoppingInfoBinding> holder, int i) {
            holder.binding.infoNum.setText(String.format("已售%s件", data.get(i).sales));
            holder.binding.infoPrice.setText(String.format("¥%s", data.get(i).price));
            holder.binding.infoTitle.setText(data.get(i).title);
            GlideApp.with(ct).load(data.get(i).cover).into(holder.binding.infoIv);
            holder.itemView.setOnClickListener(v -> startActivity(new Intent(ct, ShoppingPayActivity.class).putExtra("id", data.get(i).id)));
        }

        @Override
        public int getItemCount() {
            return null == data ? 0 : data.size();
        }
    }
}
